import { Component,OnInit } from '@angular/core';

import { Platform, AlertController } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { Keyboard } from '@ionic-native/keyboard/ngx';
import { LocalNotifications } from '@ionic-native/local-notifications/ngx';
import { Router,NavigationEnd,NavigationCancel,NavigationStart } from '@angular/router';
import { Toast } from '@ionic-native/toast/ngx';
import { RegisterButtonService } from './services/register-button.service';
import { AuthTokenService } from "@services/auth-token.service";
import { CommonResponse,VerificationType,ToastColor,CommonService,AppParam } from '@services/common-service.service';
import { AndroidPermissions } from '@ionic-native/android-permissions/ngx';
@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss']
})
export class AppComponent implements OnInit{
  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
    private keyboard:Keyboard,
    private localNotifications: LocalNotifications,
    private router: Router,
    private toast:Toast,
    private registerButtonService:RegisterButtonService,
    private authTokenService:AuthTokenService,
    private commonService:CommonService,
    private androidPermissions: AndroidPermissions
  ) {
    this.initializeApp();
  }

  ngOnInit(): void {
    
  }

  initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.overlaysWebView(true);
      this.statusBar.styleDefault();
      this.splashScreen.hide();
      this.registerButtonService.registerBackButtonAction();
      this.getCommonParams();
      
      this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.INTERNET)
      .then(result=>{
        if(result.hasPermission){
          // alert('hasPermission');
          console.info('hasPermission');
        }else{
          // alert('no hasPermission');
          console.info('no hasPermission');
        }
        
      });
    });
    // this.receiveNotification();
    let userToken = this.authTokenService.getUserToken();
    if(!userToken){
      this.router.navigate(['/auth/signin-password']);
    }
  }

  receiveNotification(){
    this.localNotifications.schedule({
      id: 1,
      title: 'notification title',
      text: 'Single ILocalNotification',
      trigger: { at: new Date(new Date().getTime() + 5000) }
    });
  }

  getCommonParams(){
    this.commonService.getAppParamFromServer(AppParam.SMS_COUNT_DOWN).subscribe((response:CommonResponse)=>{
      this.commonService.cacheAppParam(AppParam.SMS_COUNT_DOWN,response.responseBody.value);
    });
  }
}
